<template>
<div :z-index="zIndex">
    <div v-if="mode=='image'" class="images-box">
        <div v-for="(item,i) in imgList" :key="i" >
            <div class="imgs" :style="`width:${width};height:${width};margin-top:${right}};margin-right:${rightNum == i+1?'0px':right}`">
                <img :src="item" alt="">
                <div class="rowp">
                    <span @click="removeImg(i)" class="el-icon-delete"></span>
                    <span @click="isImg=true;imgSrc=item" class="el-icon-zoom-in"></span>
                </div>
            </div>
        </div>
        <div v-show="loading" class="loading" :style="[{width:width},{height:width}]">
            <i class="el-icon-loading"></i>
        </div>
        <div v-show="imgList.length < maxNum&&!loading" :style="[{width:width},{height:width}]" class="add-img">
            <span class="el-icon-plus"> </span>
            <input type="file" :disabled="loading" @change="uploadImg" title="选择要上传的图片" multiple ref="imgTop" accept="image/jpeg,image/jpg,image/png">
        </div>
    </div>
    <div v-else-if="mode=='video'" class="video-box">
        <video v-show="video" :src="video" :style="videoStye" controls preload></video>
        <div v-show="loading" class="loading" :style="[{width:width},{height:width}]">
            <i class="el-icon-loading"></i>
        </div>
        <div v-show="!video&&!loading" :style="[{width:width},{height:width}]" class="add-img">
            <span class="el-icon-plus"> </span>
            <input type="file" :disabled="loading" @change="uploadVideo" title="选择要上传的视频" multiple ref="videoTop" accept="video/*">
        </div>
    </div>
    <el-dialog custom-class="images" top="0%" :visible.sync="isImg">
        <el-image @click="isImg = false" :src="imgSrc"></el-image>
    </el-dialog>
</div>
</template>

<script>
    /**
	 * modal 图片上传
	 * @description 图片上传
	 * @tutorial https://www.uviewui.com/components/modal.html
	 * @property {Boolean} rightNum 第几个没有右边的margin
	 * @property {String | Number} z-index 层级(默认10)
     * @property {String} right margin-right数值(请带上单位px)
     * @property {String} width 图片(包括添加)的宽度(请带上单位px)
     * @property {Number} maxNum 最大上传数(默认9)
     * @property {Array} imgList 图片地址
     * @property {String} mode 模式(默认image): image图片 video视频
     * @property {String} video 视频地址
     * @property {Object} videoStye 视频样式 
	 * @event {Function} confirm 确认按钮被点击
	 * @event {Function} cancel 取消按钮被点击
	 * @example <uploadingImg :imgList="imgList"></uploadingImg>
	 */
export default {
    data(){
        return{
            loading:false,
            isImg:false,
            imgSrc:'',
        }
    },
    props:{
        videoStye:{
            type:Object,
            default(){
                return{
                    
                }
            }
        },
        mode:{
            type:String,
            default:'image'
        },
        video:{
            type:String,
            default:''
        },
        // 层级z-index
        zIndex: {
            type: [Number, String],
            default: 10
        },
        rightNum:{
            type:[Number,String],
            default:-1
        },
        right:{
            type:String,
            default:'32px'
        },
        width:{
            type:String,
            default:'150px'
        },
        maxNum:{
            type:Number,
            default:9
        },
        imgList:{
            type:[Array,String],
            default(){
                return []
            }
        }
    },
    watch:{
        imgList(val){
            this.$emit('verify',!val.length) 
        }
    },
    created(){
        
    },
    methods:{
        async uploadVideo(){//上传视频
            let type = ['image/jpeg','image/jpg','image/png']
            let files = this.$refs.videoTop.files[0];
            if(!files)return;
            if(files.size > 100*1024*1024){
                return this.$message.error('视频大小不能超过100MB');
            }
            let formData = new FormData();
            formData.append("files", files);
            this.loading = true;
            let {code,data} = await this.$Http.upload(formData);
             this.loading = false;
            if(code){
                this.$message.success("视频上传成功！")
                this.$emit('update:video',data)
                this.$refs.videoTop.value ="";
            }
        },
        removeImg(i){//删除图片
            this.imgList.splice(i,1);
        },
        uploadImg(){//图片上传  8  4
            let type = ['image/jpeg','image/jpg','image/png']
            let files = [...this.$refs.imgTop.files];
            let maxNum = this.maxNum - this.imgList.length;
            if(!files.length||!maxNum){
                return
            }else if(files.length>maxNum){
                files.splice(maxNum,files.length);
            }
            let ajaxNum = 0;
            files.forEach(async (v,i)=>{
                if(type.findIndex((v2)=>v2==v.type)!=-1){
                    let formData = new FormData();
                    formData.append("files", v);
                    this.loading = true;
                    let {code,data} = await this.$Http.upload(formData);
                    ajaxNum++;
                    if(code){
                        this.$message.success("图片上传成功！")
                        this.imgList.push(data);
                        this.$refs.imgTop.value ="";
                    }
                    if(ajaxNum==files.length){
                        // this.$loading.hide()
                        this.loading = false;
                    }
                }else this.$message.error('上传格式不正确');
               
            })
        },
    }
}
</script>

<style lang="scss" scoped>


.images-box{
    display: flex;
    flex-wrap: wrap;
    // width: 800px;
    // padding-bottom: 30px;
    // border-bottom: #F2F2F2 1px solid;
    .imgs{
        position: relative;
        width: 120px;
        height: 120px;
        border-radius: 5px;
        margin-top: 22px;
        margin-right: 32px;
        // &:nth-child(4n){
        //     margin-right: 0;
        // }
        &:hover .rowp{
            opacity: 1;
            // transition: all 1s linear;
        }
        .rowp{
            position: absolute;
            top: 0;
            width: 100%;
            height: 100%;
            background: #0005;
            display: flex;
            justify-content: space-evenly;
            align-items: center;
            color: #fff;
            opacity: 0;
            transition: opacity .3s;
            border-radius: 5px;
            span{
                padding: 5px;
                font-size: 24px;
                cursor: pointer;
            }
        }
        img{
            border-radius: 5px;
            display: block;
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

    }
}
    .loading{
        margin-top: 22px;
        display: flex;
        justify-content: center;
        align-items: center;
        .el-icon-loading{
            color: #bbb8b8;
            font-size: 34px;
            height: min-content;
        }
    }
    .add-img{
        position: relative;
        margin-top: 22px;
        display: flex;
        cursor: pointer;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        width: 150px;
        height: auto;
        color: #bbb8b8;
        border: 1px solid #D9D9D9;
        border-radius: 5px;
        input{
            width: 100%;
            height: 100%;
            position: absolute;
            top: 0;
            opacity: 0;
            cursor: pointer;
        }
        .el-icon-plus{
            // margin-bottom: 10px;
            font-size: 50px;
        }
    }

::v-deep.el-dialog__wrapper{
    // background: red;
    display: flex;
    justify-content: center;
    align-items: center;
    // height: 100vh;
    // .el-dialog{
    //     transform: translateY(-50%);
    // }
}

::v-deep.el-image{
    display: block;
    margin: 0 auto;
   
}


</style>